package main

import (
	"crypto/tls"
	"crypto/x509"
	"io/ioutil"
	"log"
	"net/http"
)

func clientStart() {
	var (
		resp   *http.Response
		err    error
		body   []byte
		tr     *http.Transport
		client *http.Client
		pool   *x509.CertPool
		cert   []byte
	)

	pool = x509.NewCertPool()
	// 读取证书（用于验证服务端证书的可靠性真实性，应该使用签发服务端证书的证书等等）
	cert, err = ioutil.ReadFile("/Users/blue7/work/go-workplace/go-https/cert/rootCA.crt")

	if err != nil {
		log.Fatalln("读取证书错误", err)
	}
	pool.AppendCertsFromPEM(cert)

	// 配置tls参数
	tr = &http.Transport{
		TLSClientConfig: &tls.Config{
			//InsecureSkipVerify: true,
			RootCAs: pool,
		},
		DisableCompression: true,
	}

	client = &http.Client{Transport: tr}
	// 请求接口
	resp, err = client.Get("https://localhost:9898/test")
	//resp, err = client.Get("https://server:9898/test")

	if err != nil {
		log.Fatalln("接口请求失败", err)
	}

	defer resp.Body.Close()
	// 读取body数据
	body, err = ioutil.ReadAll(resp.Body)

	log.Println("请求结果", string(body))
}
